{extend name="public:base"/}


{block name="title"}新增促销{/block}
{block name="style"}{/block}

{block name="main"}

<section class="content" xmlns="http://www.w3.org/1999/html">
    <div class="row">

        <div class="col-md-12">

            <div class="row">
                <div class="col-lg-12">
                    <div class="box">
                        <div class="box-header with-border">
                            <h3 class="box-title">新增促销</h3>
                        </div>
                        <form  id="pt_form" method="post">
                            <div class="box-body">

                                <div class="clearfix"></div>

     
                                <div class="form-group required">
                                    <div class='col-sm-2 text-right'>
                                        <label for="discount_name" class="control-label Validform_label" >促销名称</label>
                                    </div>
                                    <div class='col-sm-10'>
                                        <input type="text" id="discount_name" class="form-control" name="discount_name" placeholder="请输入促销名称" datatype="s2-100"  errormsg="促销名称超出长度范围">
                                        <div class="help-block"></div>
                                    </div>
                                </div>
          
                                <div class="form-group ">
                                    <div class='col-sm-2 text-right'>
                                        <label for="goods_id" class="control-label Validform_label" >选择商品</label>
                                    </div>
                                    <div class='col-sm-10'>
                                        <div class="help-block"></div>
                                        <div class="goods_list">
                                            <div class="m-t-sm"></div>
                                            <div class="search" style="height:34px;">
                                                <div class="col-sm-3">
                                                    <div class="form-row">
                                                    <span class="input-group-btn">
                                                        <button type="button"  onclick="selectGoods();" class="btn btn-white"><i class="fa fa-search"></i> 选择商品</button>
                                                    </span>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="m-t-sm"></div>

                                            <table class="table table-hover" >
                                                <thead>
                                                <th>
                                                    商品名称
                                                </th>
                                                <th>
                                                    商品图片
                                                </th>
                                                <th>
                                                    图号
                                                </th>
                                                <th>
                                                    价格
                                                </th>
                                                <th>
                                                    库存
                                                </th>
                                                <th>操作</th>
                                                </thead>
                                                <tbody id="goods_list_td">

                                                </tbody>
                                            </table>

                                        </div>
                                    </div>
                                </div>
          
                                <div class="form-group required">
                                    <div class='col-sm-2 text-right'>
                                        <label for="rate" class="control-label Validform_label" >折扣率</label>
                                    </div>
                                    <div class='col-sm-10'>
                                        <input type="text" id="rate" class="form-control" name="rate" placeholder="请选择折扣率1-100之间的整数,95代表95折" datatype="n" value="95" nullmsg="请选择折扣率">
                                        <div class="help-block"></div>
                                    </div>
                                </div>
          
                                <div class="form-group required">
                                    <div class='col-sm-2 text-right'>
                                        <label for="start_time" class="control-label Validform_label" >促销开始时间</label>
                                    </div>
                                    <div class='col-sm-10'>
                                        <input type="text" id="start_time" class="form-control" name="start_time" placeholder="请输入促销开始时间" datatype="*">
                                        <div class="help-block"></div>
                                    </div>
                                </div>
          
                                <div class="form-group required">
                                    <div class='col-sm-2 text-right'>
                                        <label for="end_time" class="control-label Validform_label" >促销结束时间</label>
                                    </div>
                                    <div class='col-sm-10'>
                                        <input type="text" id="end_time" class="form-control" name="end_time" placeholder="请输入促销结束时间" datatype="*">
                                        <div class="help-block"></div>
                                    </div>
                                </div>
                                 </div>
                            <div class="box-footer text-center">
                                <button class="btn btn-primary" type="submit">保存</button>
                                <span class="btn btn-white" onclick="history.go(-1)">返回</span>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
{/block}

{block name="js"}
<script>

    var validate = validator('#pt_form');

    window.datetimepicker_648a00bd = {"bootcssVer":3,"minView":"month","icontype":"glyphicon","fontAwesome":false,"icons":{"leftArrow":"glyphicon-arrow-left","rightArrow":"glyphicon-arrow-right"},"format":"yyyy-mm-dd","todayHighlight":true,"autoclose":true,"todayBtn":true,"timezone":"Asia\/Shanghai","language":"zh-CN"};
    jQuery('#start_time').datetimepicker(datetimepicker_648a00bd);
    jQuery('#end_time').datetimepicker(datetimepicker_648a00bd);
</script>
<script>
    Array.prototype.remove = function(val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };

    let goods_list = [];
    function joinActive(goods_id,obj){
        if(!goods_id){
            return false;
        }
        if($(obj).hasClass('btn-primary')){
            $(obj).removeClass('btn-primary').addClass('btn-danger');
            $(obj).text('取消活动');
            if(goods_list.indexOf(goods_id) == -1){
                goods_list.push(goods_id);
            }
        }else{
            $(obj).removeClass('btn-danger').addClass('btn-primary');
            $(obj).text('参与活动');
            $(obj).parent().parent().remove();
            goods_list.remove(goods_id);
        }

        $('#select_goods_id').val(goods_list);
        //console.log(goods_list);
    }

    $('#goods_list_td').html('');

    function selectGoods(){
        layer.open({
            type: 2,
            content: "<?=url('seller/goods/getGoodsList')?>",
            area:['1000px','500px'],
            success: function(layero, index){
                var body = layer.getChildFrame('body', index);
                var select_input = null;
                var iframeWin = window[layero.find('iframe')[0]['name']];
                //console.log(body.html()) //得到iframe页的body内容
                //body.find('input').val('Hi，我是从父页来的')
                select_input = body.find("input[name='select_goods_id']");
            },

            btn: ['确定', '取消'],
            yes:function(index){
                //var body = layer.getChildFrame('body');
                // let input= body.find(".select_goods_id").html();

                //let input = $("input[name='select_goods_id']:checked");
                //console.log(select_input);
                //layer.msg(input);
                var body = layer.getChildFrame('body', index);
                var select_input = null;
                //var iframeWin = window[body.find('iframe')[0]['name']];
                //console.log(body.html()) //得到iframe页的body内容
                //body.find('input').val('Hi，我是从父页来的')
                select_input = body.find("input[name='select_goods_id']:checked");
                if(select_input.length == 0){
                    layer.msg('请选择商品');
                    return false;
                }

                let goods = select_input.data('goods');
                let str = '';
                if(goods_list.indexOf(goods.goods_id) == -1){
                    goods_list.push(goods.goods_id);
                    str += '<tr>';
                    str += '<td>'+goods.goods_name+'</td>';
                    str += '<td><img style="width:80px;height:80px;" src="/'+goods.original_img+'"></td>';
                    str += '<td>'+goods.figure_nu+'</td>';
                    str += '<td>'+goods.shop_price+'</td>';
                    str += '<td>'+goods.store_count+'</td>';
                    str += '<td><button type="button" class="btn btn-danger btn-xs" title="参与活动" onclick="joinActive('+goods.goods_id+',this)">删除</button></td>';
                    str += '<input type="hidden" name="goods_id" value="'+goods.goods_id+'">';
                    str += '</tr>';
                    $('#goods_list_td').html(str);
                    $('#select_goods_id').val(goods_list);
                }
                layer.closeAll();
            }
        })
    }
</script>
{/block}